package com.zhoupu.integrated.file.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

import java.net.URISyntaxException;

@Component
public class S3Component {

    @Autowired
    private S3Config s3Config;
    @Bean
    public S3Client s3Client() throws URISyntaxException {
        AwsBasicCredentials awsCredentials = AwsBasicCredentials.create(s3Config.getAccessKey(), s3Config.getSecretKey());
        return S3Client.builder()
                .credentialsProvider(StaticCredentialsProvider.create(awsCredentials))
                .region(Region.of(s3Config.getRegion()))
                .endpointOverride(new java.net.URI(s3Config.getEndpoint()))
                .forcePathStyle(s3Config.isForcePathStyle())
                .build();
    }
}
